package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "TestNofity")
public class TestNofity {
    static final Object LOCK = new Object();

    public static void main(String[] args) {

        final Thread t1 = new Thread(() -> {
            synchronized (LOCK) {
                int i = 0;
                while (true) {
                    final Thread thread = Thread.currentThread();
                    if (thread.isInterrupted()) {
                        break;
                    }
                    if (i >= 5) {
                        break;
                    }
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        thread.interrupt();
                    }
                    log.debug("i = " + i);
                    i++;
                }
                LOCK.notify();
            }
        }, "t1");


        final Thread t2 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    log.debug("==========================> t2 LOCK.wait");
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("==========================> t2 wait");
            }
        }, "t2");

        final Thread t3 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    log.debug("==========================> t3 LOCK.wait");
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("==========================> t3 wait");
            }
        }, "t3");

        t1.start();
        t2.start();
        t3.start();
        System.out.println("main end");
    }
}
